#include "game.h"


void InitBoard(char board[ROWS][COLS],int rows,int cols,char set)
{
    int i = 0;
    int j = 0;
    for(i = 0;i < rows;i++)
    {
        for(j = 0;j < cols;j++)
        {
            board[i][j] = set;
        }
    }
}
void DisplayBoard(char board[ROWS][COLS],int row,int col)
{
    int i = 0;
    int j = 0;
    printf("*****扫雷游戏******\n");
    for(j = 0;j <= col;j++)
    {
        printf("%d ",j);
    }
    printf("\n");
    for(i = 1;i <= row;i++)
    {
        printf("%d ",i);
        for(j = 1;j <= col;j++)
        {
            printf("%c ",board[i][j]);
        }
        printf("\n");
    }
    printf("*****扫雷游戏******\n");
}
void SetMine(char board[ROWS][COLS],int row,int col)
{
    int count = EASY_COUNT;
    //横纵坐标范围都是1-9，考虑如何生成
    while(count)
    {
        int x = rand() % row + 1;//%9后为0-8，加1
        int y = rand() % col + 1;
        if(board[x][y] == '0')
        {
            board[x][y] = '1';
            count --;
        }
    }
}
int get_mine_count(char mine[ROWS][COLS],int row,int col)
{
    return (mine[row-1][col-1]+
            mine[row-1][col]+
            mine[row-1][col+1]+
            mine[row][col-1]+
            mine[row][col+1]+
            mine[row+1][col-1]+
            mine[row+1][col]+
            mine[row+1][col+1] - 8 * '0');//也可以写成循环的形式
}
void FindMine(char mine[ROWS][COLS],char show[ROWS][COLS],int row,int col)
{
    int x = 0;
    int y = 0;
    int win = 0;//排查过的格子个数
    //当把除了雷之外的格子全部排查完就结束循环
    while(win < row * col - EASY_COUNT)
    {
        printf("请输入要排查的坐标：>\n");
        scanf("%d %d",&x,&y);
        if(x >= 1 && x <= 10 && y >= 1 && y <= 10)
        {
            if(show[x][y] != '*')
            //若已排查，重新输入
            {
                printf("该坐标已排查，请重新输入");
            }
            else
            {
                //如果是雷
                if(mine[x][y] == '1')
                {
                    printf("很遗憾，你被炸死了\n");
                    DisplayBoard(mine,ROW,COL);
                    break;
                }
                //如果不是雷，统计周围8个格子的雷数
                else
                {
                    win ++;
                    int count = get_mine_count(mine,x,y);
                    show[x][y] = count + '0';//+'0'字符0可以实现从数值到字符的转换
                    DisplayBoard(show,ROW,COL);
                }
            } 
        }  
        else
        {
            printf("坐标错误，请重新输入\n");
        }
        if(win == row * col - EASY_COUNT)
        {
            printf("恭喜你，扫雷成功\n");
        }       
    }
}

